import React, { Component } from "react";
import { Link } from "react-router-dom";
import Header from "../../components/Header/Header";
import { Button, Form, Input, Toast } from "antd-mobile";
import { reqlogin } from "../../http/api";
export default class Login extends Component {
  constructor() {
    super();
    this.state = {
      user: {
        phone: "",
        password: "",
      },
    };
  }
  //登录
  async login() {
    let res = await reqlogin(this.state.user);
    if (res.data.code === 200) {
      Toast.show({
        content: res.data.msg,
      });
      //存信息
      sessionStorage.setItem("userInfo",JSON.stringify(res.data.list))
      //跳页面
      this.props.history.push("/index/home");
    }
  }
  render() {
    let { user } = this.state;
    return (
      <div>
        <Header register title="登录"></Header>
        <div>user:{JSON.stringify(user)}</div>
        <Form layout="horizontal">
          <Form.Item label="手机号" name="username">
            <Input
              placeholder="请输入手机号"
              clearable
              onChange={(v) => {
                this.setState({
                  user: {
                    ...this.state.user,
                    phone: v,
                  },
                });
              }}
            />
          </Form.Item>
          <Form.Item label="密码" name="password">
            <Input
              placeholder="请输入密码"
              clearable
              type="password"
              onChange={(v) =>
                this.setState({ user: { ...this.state.user, password: v } })
              }
            />
          </Form.Item>
        </Form>
        <Button size="small" color="primary" onClick={() => this.login()}>
          登录
        </Button>
        <Link to="/index/home">前往首页</Link>
      </div>
    );
  }
}
